﻿package framework.core;


public class SearchEvent 
{
	public static final String PREFIX = "s_" ;
	public static final int DEFAULT_PAGE_SIZE = 20 ;

	private int m_page_size = DEFAULT_PAGE_SIZE ;
	private int m_page_num = 1 ;
	private String m_order_field = "";
	private boolean m_order_desc = true;

	//控制第一次查询时是否出查询结果
	private boolean m_init = true;
	public boolean isInit() 
	{
		return m_init ;
	}
	public void setInit(boolean v)
	{
		m_init = v;
	}
	
	private SearchEventRule m_rule = new SearchEventRule();
	public SearchEventRule getRule()
	{
		return m_rule;
	}
	public void setRule(SearchEventRule rule)
	{
		m_rule = rule ;
	}
	
public SearchEvent() 
{
	super();
}
	public String getOrderField()
	{
		return m_order_field;
	}
	public int getPageNum()
	{
		return m_page_num;
	}
	public int getPageSize()
	{
		return m_page_size;
	}
	public boolean isOrderDesc()
	{
		return m_order_desc;
	}
	public void setOrderDesc(boolean v)
	{
		m_order_desc = v;
	}
	public void setOrderField(String v)
	{
		m_order_field = v;
	}
	public void setPageNum(int v)
	{
		m_page_num = v;
	}
	public void setPageSize(int v)
	{
		if(v>0)
		{
			m_page_size = v ;
		}
		else if(v==Value.NULL_INT)
		{
			m_page_size=Value.NULL_INT;
		}
		else	
		{
			m_page_size = DEFAULT_PAGE_SIZE;
		}
	}
	
	//proxy interface
	public Condition getCondition(String sql) throws Exception
	{
		return getRule().getCondition(sql);
	}
	public Condition getCondition(String fieldName,int v) throws Exception
	{
		return getCondition(Conditions.EQUAL,fieldName,v); 
	}
	public Condition getCondition(String fieldName,double v) throws Exception
	{
		return getCondition(Conditions.EQUAL,fieldName,v);
	}
	public Condition getCondition(String fieldName,String v) throws Exception
	{
		return getCondition(Conditions.LIKE,fieldName,v);
		//return getCondition(Conditions.EQUAL,fieldName,v);
	}
	public Condition getCondition(String fieldName,Value v) throws Exception
	{
		return getCondition(Conditions.EQUAL,fieldName,v);
	}
	public Condition getCondition(int type,String fieldName,int v) throws Exception
	{
		return getRule().getCondition(type,fieldName,v);
	}
	public Condition getCondition(int type,String fieldName,double v) throws Exception
	{
		return getRule().getCondition(type,fieldName,v);
	}
	public Condition getCondition(int type,String fieldName,String v) throws Exception
	{
		return getRule().getCondition(type,fieldName,v);
	}
	public Condition getCondition(int type,String fieldName,Value v) throws Exception
	{
		return getRule().getCondition(type,fieldName,v);
	}
	public Condition getCondition(int type,String fieldName,int[] v) throws Exception
	{
		return getRule().getCondition(type,fieldName,v);
	}
	public Condition getCondition(int type,String fieldName,double[] v) throws Exception
	{
		return getRule().getCondition(type,fieldName,v);
	}
	public Condition getCondition(int type,String fieldName,String[] v) throws Exception
	{
		return getRule().getCondition(type,fieldName,v);	
	}
	public Condition getCondition(int type,String fieldName,Values v) throws Exception
	{
		return getRule().getCondition(type,fieldName,v);
	}
	public Conditions toConditions() throws Exception
	{
		Conditions c = new Conditions();
		if(!isInit())
		{
			c.add("1=0");
		}
		return c;
	}
	////从页面提交的数据得到SearchEvent
	public static DBData fromFormInitPojo(SrvContext form,Class c) throws Exception
	{   
		DBData  cls = null ;
	    try 
	    {
	        Class[] cs = {};
	        Object[] os = {};
	        cls = (DBData) c.getConstructor(cs).newInstance(os);
	    } 
	    catch (Exception e) 
	    {
	        Logger.throwException("SearchEvent.fromForm","实例化对象错误:className="+c.getName());
	    }
	
	    java.lang.reflect.Method[] ms = cls.getClass().getMethods();
	
	    for (int i = 0; i < ms.length; i++) 
	    {
	        try 
	        {
	            java.lang.reflect.Method m = ms[i];
	            String name = m.getName();
	            String value = "";
	            if (name.indexOf("set") == 0) 
	            {
	                name = name.substring(3);
	                if (name.length() > 0) 
	                {
	                    value = form.getString(SearchEvent.PREFIX+name);
	                    if (value == null) continue;
	                    if (value.length() == 0) continue;
	                    value = value.trim();
	
	                    if (m.getParameterTypes()[0].equals(String.class)) 
	                    {
	                        Object[] os = { value };
	                        m.invoke(cls, os);
	                    }
	                    else if (m.getParameterTypes()[0].equals(boolean.class)) 
	                    {
		                 	try
		                 	{
		                    	Boolean v = Boolean.valueOf(value);
	                        	Object[] os = {v};
	                        	m.invoke(cls, os);
		                 	}
		                 	catch(Exception e)
		                 	{
		                 	}
	                    }
	                    else if (m.getParameterTypes()[0].equals(int.class)) 
	                    {
	                    	try
	                    	{
		                    	Integer v = Integer.valueOf(value);
		                        Object[] os = {v};
	    	                    m.invoke(cls, os);
	                    	}
	                    	catch(Exception e)
	                    	{
	                    	}
	                    }
	                    else if (m.getParameterTypes()[0].equals(double.class)) 
	                    {
	                    	try
	                    	{
		                    	Double v = Double.valueOf(value);
	                        	Object[] os = {v};
		                        m.invoke(cls, os);
	                    	}
	                    	catch(Exception e)
	                    	{
	                    	}
	                    }
	                    else if (m.getParameterTypes()[0].equals(java.sql.Date.class)) 
	                    {
		                    java.sql.Date v = java.sql.Date.valueOf(value);
	                        Object[] os = {v};
	                        m.invoke(cls, os);
	                    }
	                    else if (m.getParameterTypes()[0].equals(DateUtil.class)) 
	                    {
		                    DateUtil v = new DateUtil(value);
	                        Object[] os = {v};
	                        m.invoke(cls, os);
	                    }
	                    else
	                    {
		                    //cannot be here 
	                    }
	                }
	            }
	        } 
	        catch (Exception e) 
	        {
	            Logger.throwException("SearchEvent.fromForm()",e.getMessage());
	        }
	    }
	    return cls;
	}
	
	////从页面提交的数据得到SearchEvent
	public static SearchEvent fromForm(SrvContext form,Class c) throws Exception
	{
	    SearchEvent cls = null ;
	    try 
	    {
	        Class[] cs = {};
	        Object[] os = {};
	        cls = (SearchEvent) c.getConstructor(cs).newInstance(os);
	    } 
	    catch (Exception e) 
	    {
	        Logger.throwException("SearchEvent.fromForm","实例化对象错误:className="+c.getName());
	    }
	
	    java.lang.reflect.Method[] ms = cls.getClass().getMethods();
	
	    for (int i = 0; i < ms.length; i++) 
	    {
	        try 
	        {
	            java.lang.reflect.Method m = ms[i];
	            String name = m.getName();
	            String value = "";
	            if (name.indexOf("set") == 0) 
	            {
	                name = name.substring(3);
	                if (name.length() > 0) 
	                {
	                    value = form.getString(SearchEvent.PREFIX+name);
	                    if (value == null) continue;
	                    if (value.length() == 0) continue;
	                    value = value.trim();
	
	                    if (m.getParameterTypes()[0].equals(String.class)) 
	                    {
	                        Object[] os = { value };
	                        m.invoke(cls, os);
	                    }
	                    else if (m.getParameterTypes()[0].equals(boolean.class)) 
	                    {
		                 	try
		                 	{
		                    	Boolean v = Boolean.valueOf(value);
	                        	Object[] os = {v};
	                        	m.invoke(cls, os);
		                 	}
		                 	catch(Exception e)
		                 	{
		                 	}
	                    }
	                    else if (m.getParameterTypes()[0].equals(int.class)) 
	                    {
	                    	try
	                    	{
		                    	Integer v = Integer.valueOf(value);
		                        Object[] os = {v};
	    	                    m.invoke(cls, os);
	                    	}
	                    	catch(Exception e)
	                    	{
	                    	}
	                    }
	                    else if (m.getParameterTypes()[0].equals(double.class)) 
	                    {
	                    	try
	                    	{
		                    	Double v = Double.valueOf(value);
	                        	Object[] os = {v};
		                        m.invoke(cls, os);
	                    	}
	                    	catch(Exception e)
	                    	{
	                    	}
	                    }
	                    else if (m.getParameterTypes()[0].equals(java.sql.Date.class)) 
	                    {
		                    java.sql.Date v = java.sql.Date.valueOf(value);
	                        Object[] os = {v};
	                        m.invoke(cls, os);
	                    }
	                    else if (m.getParameterTypes()[0].equals(DateUtil.class)) 
	                    {
		                    DateUtil v = new DateUtil(value);
	                        Object[] os = {v};
	                        m.invoke(cls, os);
	                    }
	                    else
	                    {
		                    //cannot be here 
	                    }
	                }
	            }
	        } 
	        catch (Exception e) 
	        {
	            Logger.throwException("SearchEvent.fromForm()",e.getMessage());
	        }
	    }
	    return cls;
	}
}
